<template>
  <div>
    <dialogDiy
      ref="dialogDiy"
      :dialogVisible="dialogVisible"
      @handleClose="handleClosePrent"
    >
      <div class="topContent">新建条件及选项</div>
      <div class="centerContent">
        <el-form :inline="true" :model="formData" class="demo-form-inline">
          <el-row>
            <el-col :span="12">
              <el-form-item label="条件名称">
                <el-input
                  class="inputClass"
                  v-model="formData.conditionsName"
                  placeholder="请输入新建条件名称"
                  clearable
                  maxlength="20"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="条件key值">
                <el-input
                  class="inputClass"
                  v-model="formData.conditionsValue"
                  maxlength="20"
                  clearable
                  placeholder="请输入条件件值"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="匹配步骤">
                <el-select
                  v-model="formData.conditionStep"
                  class="inputClass"
                  placeholder="请选择"
                  multiple
                >
                  <el-option
                    v-for="item in conditionStepList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="条件类型">
                <el-checkbox-group v-model="formData.conditionType">
                  <el-checkbox value="1" label="1">企业</el-checkbox>
                  <el-checkbox value="2" label="2">个体工商户</el-checkbox>
                </el-checkbox-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="选项类型">
                <el-select
                  v-model="formData.selectType"
                  placeholder="请选择"
                  class="inputClass"
                >
                  <el-option
                    v-for="item in selectTypeList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="判断逻辑">
                <el-select
                  v-model="formData.judge"
                  placeholder="请选择"
                  class="inputClass"
                >
                  <el-option
                    v-for="item in judgeList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否为必要条件">
                <el-radio-group v-model="formData.must">
                  <el-radio :label="1">是</el-radio>
                  <el-radio :label="0">否</el-radio> 
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <template>
            <div style="max-height: 400px; overflow-y: auto">
              <el-table :data="tableData" size="mini" border>
                <!--                <el-table-column prop="" label="ID" width="100">-->
                <!--                  <template slot-scope="scope">-->
                <!--                   -->
                <!--                    <span>{{ scope.row.id }}</span>-->
                <!--                  </template>-->
                <!--                </el-table-column>-->
                <el-table-column align="center" prop="label" label="条件选项">
                  <template slot-scope="scope">
                    <el-input
                      v-model="scope.row.label"
                      placeholder="请输入条件件值"
                    ></el-input>
                  </template>
                </el-table-column>

                <el-table-column
                  prop="value"
                  label="未知数"
                  v-if="
                    formData.selectType === 'OP_SCOPE' ||
                    formData.selectType === 'OP_EQUAL'
                  "
                >
                  <template slot-scope="scope">
                    <el-input
                      controls-position="right"
                      v-model="scope.row.unknowns"
                      placeholder="请输入新建条件名称"
                    ></el-input>
                  </template>
                </el-table-column>

                <el-table-column
                  align="center"
                  label="选项值"
                  v-if="
                    formData.selectType !== 'OP_SCOPE' &&
                    formData.selectType !== 'OP_EQUAL'
                  "
                >
                  <el-table-column prop="value" align="center" label="value">
                    <template slot-scope="scope">
                      <el-input
                        controls-position="right"
                        v-model="scope.row.value"
                        placeholder="请输入新建条件名称"
                      ></el-input>
                    </template>
                  </el-table-column>
                  <el-table-column
                    prop="valueMin"
                    align="center"
                    label="valueMin"
                  >
                    <template slot-scope="scope">
                      <el-input
                        type="number"
                        controls-position="right"
                        v-model="scope.row.valueMin"
                        placeholder="请输入新建条件名称"
                      ></el-input>
                    </template>
                  </el-table-column>
                  <el-table-column
                    prop="valueMax"
                    align="center"
                    label="valueMax"
                  >
                    <template slot-scope="scope">
                      <el-input
                        type="number"
                        controls-position="right"
                        v-model="scope.row.valueMax"
                        placeholder="请输入新建条件名称"
                      ></el-input>
                    </template>
                  </el-table-column>
                </el-table-column>

                <el-table-column
                  prop="switchTo"
                  align="center"
                  label="实现分支"
                >
                  <template slot-scope="scope">
                    <el-input
                      type="number"
                      controls-position="right"
                      v-model="scope.row.switchTo"
                      placeholder="请输入新建条件名称"
                    ></el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="" align="center" width="300">
                  <template slot="header" slot-scope="scope">
                    <span @click="addlistData">
                      <i
                        style="color: #409eff; font-size: 20px"
                        class="el-icon-circle-plus"
                      ></i>
                    </span>
                  </template>
                  <template slot-scope="scope">
                    <el-button
                      size="small"
                      @click="deleteListData(scope.row)"
                      type="primary"
                      >删除</el-button
                    >
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </template>
          <el-row>
            <el-form-item label="调用方法" style="margin-top: 20px">
              <el-input
                style="width: 500px"
                v-model="formData.callingMethods"
                clearable
                placeholder="请输入新建条件名称"
              ></el-input>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="条件分组">
              <el-select
                v-model="formData.conditions_group"
                placeholder="活动区域"
                style="width: 500px"
              >
                <el-option
                  v-for="item in conditions_groupList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-row>
          <div class="positionStyle">
            <el-button type="primary" @click="onSubmit">确定添加</el-button>
            <el-button @click="handleClose">取消</el-button>
          </div>
        </el-form>
        <div style="height: 40px"></div>
      </div>
    </dialogDiy>
  </div>
</template>
<script>
import dialogDiy from "../../../components/dialogDiy/index.vue";
import { getDictData, dictTypeFunList } from "@/utils/dict";
import { createResource } from "@/api/condition";
export default {
  name: "addDialog",
  components: {
    dialogDiy,
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      handleNum: "",
      callback: function () {},
      checked: [],
      title: "",
      // dialogVisible: true,
      DialogWidth: "1200px",
      modalDes: true,
      formData: {
        conditionsName: "", //条件名称
        conditionsValue: "", //条件件值
        conditionStep: [], //匹配步骤
        conditionType: ["1", "2"], //条件类型
        selectType: "", //选项类型
        judge: "", //判断逻辑
        conditions_group: "", //条件分组
        callingMethods: "", //调用方法
        isRequired:"",
      },
      tableData: [
        {
          label: "",
          value_min: "",
          value: "",
          value_max: "",
          switchTo: "1",
          unknowns: "", //占位符
        },
      ],
      conditionStepList: [],
      selectTypeListselectTypeList: [],
      judgeList: [],
      conditions_groupList: [],
    };
  },
  mounted() {
    // this.openlog()
    this.init(); //初始化下拉框
  },
  methods: {
    init() {
      //匹配步骤
      getDictData("match_step").then((res) => {
        this.conditionStepList = res;
      });
      //选项类型
      getDictData("value_type").then((res) => {
        this.selectTypeList = res;
      });
      //判断逻辑
      getDictData("valid_conditions").then((res) => {
        this.judgeList = res;
      });
      //条件分组
      getDictData("conditions_group").then((res) => {
        this.conditions_groupList = res;
      });
    },
    openlog() {
      this.$refs.dialogDiy.openlog();
    },
    handleClosePrent() {
      this.formData = {
        conditionsName: "", //条件名称
        conditionsValue: "", //条件件值
        conditionStep: [], //匹配步骤
        conditionType: [], //条件类型
        selectType: "", //选项类型
        judge: "", //判断逻辑
        conditions_group: "", //条件分组
        callingMethods: "", //调用方法
      };
      this.tableData = [
        {
          label: "",
          value_min: "",
          value: "",
          value_max: "",
          switchTo: "",
        },
      ];
    },
    handleClose() {
      this.formData = {
        conditionsName: "", //条件名称
        conditionsValue: "", //条件件值
        conditionStep: [], //匹配步骤
        conditionType: [], //条件类型
        selectType: "", //选项类型
        judge: "", //判断逻辑
        conditions_group: "", //条件分组
        callingMethods: "", //调用方法
      };
      this.tableData = [
        {
          label: "",
          value_min: "",
          value: "",
          value_max: "",
          switchTo: "",
        },
      ];
      this.$refs.dialogDiy.handleClose();
    },
    addlistData() {
      this.tableData.push({
        label: "",
        conditions: "",
        createBy: "",
        createTime: "",
        matchStep: "",
        valueType: "",
        validConditions: "",
        refTo: "",
        groupId: "''",
        value_min: "",
        value: "",
        value_max: "",
        switchTo: "",
      });
    },
    deleteListData(val) {
      this.tableData.forEach((item, index) => {
        if (JSON.stringify(item) === JSON.stringify(val)) {
          this.tableData.splice(index, 1);
        }
      });
    },
    onSubmit() {
      let params = {
        conditionsName: this.formData.conditionsName,
        conditions: this.formData.conditionsValue,
        createBy: "",
        createTime: "",
        matchType: this.formData.conditionType.join(","),
        matchStep: this.formData.conditionStep.join(","),
        valueType: this.formData.selectType,
        validConditions: this.formData.judge,
        refTo: this.formData.callingMethods,
        groupId: this.formData.conditions_group,
        conditionsList: this.tableData,
        must:this.formData.must
      };
      createResource(params).then((res) => {
        if (res.code == 200) {
          this.$message({
            message: "新增成功",
            type: "success",
          });
          this.handleClose();
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
/deep/ .el-dialog__body {
  padding: 0 20px;
}

.topContent {
  font-size: 16px;
  color: black;
  text-align: center;
  margin-bottom: 20px;
}

.inputClass {
  width: 409px;
}

.positionStyle {
  margin-left: 40%;
}
</style>
